﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Gene
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Hide();
            button2.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string plaintext = textBox1.Text;
            string key = textBox2.Text;
            int i = 122;
            textBox3.Text = Clib.Feistel(plaintext, key);

            progressBar1.Maximum = Convert.ToInt16(plaintext);
            for (int j = 0; j <= Convert.ToInt16(plaintext); ++j)
            {
                progressBar1.Value = j;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            String s = textBox1.Text;

            SaveFileDialog savefile = new SaveFileDialog();
            savefile.Title = "Where do you want to save your file?";
            savefile.RestoreDirectory = true;
            savefile.DefaultExt = "doc";
            savefile.AddExtension = true;
            savefile.ShowHelp = true;
            savefile.Filter = "JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif|PNG Image|*.png|Word Doc|*.doc|Text Doc|*.txt|Rich Text Doc|*.rtf";
            savefile.SupportMultiDottedExtensions = true;
            if (savefile.ShowDialog() == DialogResult.OK)
            {
                if (savefile.CheckFileExists == true)
                {
                    bool b = MessageBox.Show("The name you choose is already taken, Overwrite?", "Warning!", MessageBoxButtons.YesNo) == DialogResult.Yes;
                    if (b)
                    {
                        //overwrite
                        File.Delete(savefile.FileName);
                        Clib.CreateFile(savefile.FileName, Clib.ReadOnly(textBox1.Text));
                    }
                    else
                    {
                        //do nothing
                    }
                }
                else
                {
                    //saving
                    Clib.CreateFile(savefile.FileName, Clib.ReadOnly(textBox1.Text));
                }

            }
            else
            {
                MessageBox.Show("Cancelling, Kid?","Aloha!");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();
            openfile.Title = "Find your file here";
            openfile.RestoreDirectory = true;
            openfile.AddExtension = true;
            if (openfile.ShowDialog() == DialogResult.OK)
            {
                if (openfile.CheckFileExists == false && openfile.CheckPathExists == false)
                {
                    MessageBox.Show("File yang dicari tidak ada","Warning!",MessageBoxButtons.OK);
                }
                else
                {
                    //open
                    byte[] b = Clib.ReadFile(openfile.FileName);
                    StringBuilder s = new StringBuilder();
                    for (int i = 0; i <= b.Length - 1; ++i)
                    {
                        s.Append((char)b[i]);
                    }
                    textBox1.Text = s.ToString();

                    Clib.printConsole(b);
                }
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                button2.Hide();
                button1.Show();
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                button1.Hide();
                button2.Show();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

  
    }
}
